package com.factors.HuangHePing.SchoolTypeCount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class SchoolTypeScoreDriver {

    public static void main(String[] args) throws Exception {
        //配置文件对象
        Configuration conf = new Configuration();

        // 创建作业实例
        Job job = Job.getInstance(conf, "School Type Exam Score Analysis");

        // 设置作业驱动类
        job.setJarByClass(SchoolTypeScoreDriver.class);

        // 分别设置了Mapper、Reducer 和 Partitioner 类。
        // Mapper负责处理输入数据并生成中间键值对，
        // Reducer接收来自Mapper的输出并进行聚合或其他处理，
        // Partitioner决定中间键值对应该发送到哪个Reducer。
        job.setMapperClass(SchoolTypeScoreMapper.class);
        job.setReducerClass(StudentCountReducer.class);
        job.setPartitionerClass(SchoolTypePartitioner.class);

        //指定了最终输出的键和值的数据类型。
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path("Data/part-m-00000"));
        FileOutputFormat.setOutputPath(job, new Path("output/Huang/SchoolTypeCount"));

        //判断输出路径是否存在 如果存在删除
        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(new Path("output/Huang/SchoolTypeCount"))){
            fs.delete(new Path("output/Huang/SchoolTypeCount"),true);
        }
        // 提交作业并等待执行完成
        boolean resultFlag = job.waitForCompletion(true);
        //程序退出
        System.exit(resultFlag ? 0 :1);
    }
}